package com.zz.utils;

import com.zz.dto.SexCount;
import com.zz.pojo.Out;
import com.zz.pojo.Patient;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * Copyright Zz
 * FileName: IndexUtil
 * Author:   Zz
 * Date:     2023/6/40:09
 * Description:首页信息生成的工具类，完成信息生成算法
 * hISTORY:
 * <author>  <time>     <version>    <desc>
 * 作者姓名   修改时间       版本号       描述
 */
public class IndexUtil {
    /**
     * @param outList     所有的出院信息
     * @param patientList 所有的在院信息
     * @return 入住总人数
     */
    public static int getOkCount(List<Out> outList, List<Patient> patientList) {
        Set<String> set = new HashSet<>();

        for (Patient patient : patientList) {
            set.add(patient.getPatientId());
        }

        for (Out out : outList) {
            set.add(out.getPatientID());
        }
        return set.size();
    }

    /**
     * @param patientList 所有的在院信息
     * @return 在站男女比例(eg : 21 : 30)
     */
    public static String getpeopleCount(List<Patient> patientList) {
        int man = 0;
        int woman = 0;

        for (Patient patient : patientList) {
            String sex = patient.getPatientSex();
            if (sex.equals("男"))
                man++;
            else if (sex.equals("女"))
                woman++;
        }
        return man + ":" + woman;
    }

    /**
     * @param patientList 所有的在院信息
     * @return 今日入住人数
     */
    public static int getInCount(List<Patient> patientList) {
        int count = 0;
        LocalDate date = LocalDate.now();

        for (Patient patient : patientList) {
            if (patient.getPatientInTime().equals(date))
                count++;
        }
        return count;
    }

    /**
     * @param outList     所有的出院信息
     * @param patientList 所有的在院信息
     * @return 累计男女比
     */
    public static SexCount[] getSexd(List<Out> outList, List<Patient> patientList) {
        Set<String> set1 = new HashSet<>();

        Set<String> set2 = new HashSet<>();

        for (Patient patient : patientList) {
            String sex = patient.getPatientSex();
            if (sex.equals("男"))
                set1.add(patient.getPatientId());
            else if (sex.equals("女"))
                set2.add(patient.getPatientId());
        }

        for (Out out : outList) {
            String sex = out.getPatientSex();
            if (sex.equals("男"))
                set1.add(out.getPatientID());
            else if (sex.equals("女"))
                set2.add(out.getPatientID());
        }

        SexCount[] sexd=new SexCount[2];
        sexd[0]=new SexCount("男",set1.size());
        sexd[1]=new SexCount("女",set2.size());
        return sexd;
    }

    /**
     * @param outList     所有的出院信息
     * @param patientList 所有的在院信息
     * @return 近7天的新增、康复以及累计总人数
     */
    public static List<int[]> getBarData(List<Out> outList, List<Patient> patientList, int okCount) {
        List<int[]> list = new ArrayList<>();
        int[] barDate = new int[7];
        int[] barDate2 = new int[7];
        int[] barDate3 = new int[7];

        LocalDate now = LocalDate.now();

        for (Patient patient : patientList) {
            LocalDate date = patient.getPatientInTime();
            long days = date.until(now, ChronoUnit.DAYS);
            if (0 <= days && days <= 6)
                barDate[(int) (6 - days)]++;
        }

        for (Out out : outList) {
            LocalDate date = out.getPatientOutTime();
            long days = date.until(now, ChronoUnit.DAYS);
            if (0 <= days && days <= 6)
                barDate2[(int) (6 - days)]++;
        }

        barDate3[6] = okCount;
        for (int i = 5; i >= 0; i--) {
            barDate3[i] = okCount - barDate[i + 1];
            okCount -= barDate[i + 1];
        }

        list.add(barDate);
        list.add(barDate2);
        list.add(barDate3);
        return list;
    }
}


